package restful.api;

import java.util.HashMap;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import restful.entity.Order;
import restful.entity.Product;

@Path("/sale")
public class CaseSale {
	// 参数占位符
	@GET
	// http://127.0.0.1:8080/caseSale/sale/act/4/2?pay=20
	@Path("/act/{productId}/{amount}")
	@Produces("application/json;charset=UTF-8")  
	public Order test(
			@PathParam("productId") int productId,  //客户端损失
            @PathParam("amount") int amount,  
            @QueryParam("pay") float pay
			) 
	{
		// int value1=Integer.parseInt(value1); //服务器损失
		HashMap priMap,proMap,codeMap;
		// 获取价格hash表
		priMap=getPriceMap();
		// 获取商品hash表
		proMap=getProductMap();
		// 获取状态码信息
		codeMap=getStuteCodeMap();
		// 获取对应商品信息
		Product product =new Product();
		Order payOrder = new Order();
		try {
		product.setProductId(productId);
		product.setProductName(proMap.get(productId).toString());
		product.setPrice((float) priMap.get(proMap.get(productId)));
		}catch(NullPointerException e) {
			//如果错误创建订单
			payOrder.setName("未注册商品");
			payOrder.setAmount(amount);
			payOrder.setChange(pay);
			payOrder.setResultCode(-300);
			payOrder.setResultText("无此商品售卖，全额退款");
			return payOrder;
		}
		//获取支付状态，找零数组
		float[] payStu=pay(amount,product.getPrice(), pay, product.getProductId());	
		// 创建订单
		payOrder.setName(product.getProductName());
		payOrder.setAmount(amount);
		payOrder.setChange(payStu[1]);
		payOrder.setResultCode((int)payStu[0]);
		payOrder.setResultText(codeMap.get((int)payStu[0]).toString());
		return payOrder;
		
	}
	
	/***
	 * 
	 * @return
	 */
	public static HashMap<String, Float> getPriceMap() {
		HashMap<String, Float> priceMap = new HashMap<String, Float>();
		priceMap.put("可乐", (float) 3);
		priceMap.put("雪碧", (float) 3);
		priceMap.put("橙汁", (float) 2.5);
		priceMap.put("玉泉+C", (float) 7);
		priceMap.put("酱油 ", (float) 14);
		priceMap.put("水动乐 ", (float) 4);
		priceMap.put("醋", (float) 18);
		priceMap.put("铅笔", (float) 1);
		return priceMap;
	}
	
	/**
	 * 
	 * @return
	 */
	public static HashMap<Integer, String> getProductMap() {
		HashMap<Integer, String> productMap = new HashMap<Integer, String>();
		productMap.put(0,"可乐");
		productMap.put(1,"雪碧");
		productMap.put(2,"橙汁");
		productMap.put(3,"玉泉+C");
		productMap.put(4,"酱油 ");
		productMap.put(5,"水动乐");
		productMap.put(6,"醋");
		productMap.put(7,"铅笔");
		return productMap;
	}
	
	/**
	 * 
	 * @return
	 */
	public static HashMap<Integer, Integer> getQuantityMap() {
		HashMap<Integer, Integer> productMap = new HashMap<Integer, Integer>();
		productMap.put(0,5);
		productMap.put(1,5);
		productMap.put(2,5);
		productMap.put(3,5);
		productMap.put(4,5);
		productMap.put(5,5);
		productMap.put(6,4);
		productMap.put(7,3);
		return productMap;
	}
	
	/**
	 * 
	 * @return
	 */
	public static HashMap<Integer, String> getStuteCodeMap() {
		HashMap<Integer, String> statusCodeMap = new HashMap<Integer, String>();
		statusCodeMap.put(0,"成功售卖");
		statusCodeMap.put(-100,"金额不足，全额退款；");
		statusCodeMap.put(-200,"商品库存不足，全额退款；");
		statusCodeMap.put(-300,"无此商品售卖，全额退款；");
		return statusCodeMap;
	}
	
	/**
	 * 
	 * @param amount
	 * @param price
	 * @param pay
	 * @return  payStaus
	 */
	public static float[] pay(int amount,float price, float pay, int pid) {
		float[] payStatus= new float[2];
		HashMap quanMap,proMap;
		quanMap = getQuantityMap();
		proMap = getProductMap();
		if(amount*price>pay) {
			payStatus[0]=-100;
			payStatus[1]=pay;
		}else if(amount>(int)quanMap.get(pid)) {
			payStatus[0]=-200;
			payStatus[1]=pay;
		}else {
			payStatus[0]=0;
			payStatus[1]=pay-amount*price;
		}
		
		return payStatus;

	}
}
